﻿using System;
using System.Linq;
using BrainTechLLC;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using ClassesToSerialize;
using AltSerialize;

namespace Silverlight.AltSerializer.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        public static Random _rand = new Random();

        [OperationContract]
        public byte[] DoWork()
        {
            List<DemoMonster> results = new List<DemoMonster>();

            for (int n = 0; n < 3000; n++)
            {
                var monster = CreateDemoMonster();

                for (int i = 0; i < 4; i++)
                {
                    var item = CreateDemoItem();
                    monster._items.Add(item);
                }

                results.Add(monster);
            }

            Serializer s = new Serializer();
            byte[] bytes = s.Serialize(results, typeof(List<DemoMonster>));
            byte[] compressed = bytes.Compress();

            return compressed;
        }

        private DemoMonster CreateDemoMonster()
        {
            DemoMonster monster = new DemoMonster()
            {
                _level = _rand.Next(1, 20000),
                _name = DemoMonster.CreateDummyName()
            };

            monster.Stats.Health = (short)_rand.Next(20, 500);
            monster.Stats.MaxHealth = monster.Stats.Health;

            return monster;
        }

        private Item CreateDemoItem()
        {
            Item item = new Item()
            {
                _name = Item.CreateDummyName(),
                _uses = (short)_rand.Next(0, 1000),
                _wearType = (ItemWearType)_rand.Next(0, 5)
            };

            return item;
        }
    }
}
